﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FACUI.BasicClass
{
    /// <summary>
    /// 区段
    /// </summary>
    public class Quduan
    {
        public int AD;
        public static int adAutoCnt = 0;
        public List<DC> lst道岔;
        public List<KedaDuandian> lst可达组合;
        public List<MapLinkRelation> lst连接关系;
        public List<Duandian> lst区段端点;
        public List<WCQD> lst无岔区段;
        public string name;

        public Quduan(int AD)
        {
            this.lst连接关系 = new List<MapLinkRelation>();
            this.AD = 0;
            this.name = "未知";
            this.lst区段端点 = new List<Duandian>();
            this.lst无岔区段 = new List<WCQD>();
            this.lst道岔 = new List<DC>();
            this.lst可达组合 = new List<KedaDuandian>();
            this.AD = AD;
        }

        public Quduan(string stringNode)
        {
            this.lst连接关系 = new List<MapLinkRelation>();
            this.AD = 0;
            this.name = "未知";
            this.lst区段端点 = new List<Duandian>();
            this.lst无岔区段 = new List<WCQD>();
            this.lst道岔 = new List<DC>();
            this.lst可达组合 = new List<KedaDuandian>();
            string[] strArray = stringNode.Split(new char[] { ',' });
            this.AD = int.Parse(strArray[0]);
            this.name = strArray[1];
        }

        public Quduan(int AD, string name)
        {
            this.lst连接关系 = new List<MapLinkRelation>();
            this.AD = 0;
            this.name = "未知";
            this.lst区段端点 = new List<Duandian>();
            this.lst无岔区段 = new List<WCQD>();
            this.lst道岔 = new List<DC>();
            this.lst可达组合 = new List<KedaDuandian>();
            this.AD = AD;
            this.name = name;
        }
        private bool digui(List<Sharp> lll, Point pointFrom, int jiaodu, Point pointTo, int jiaodutu)
        {
            foreach (Sharp sharp in lll)
            {
                char ch;
                if (!sharp.岔后生成了么 && sharp.hasPoint(pointFrom, out ch))
                {
                    int num;
                    sharp.岔后生成了么 = true;
                    sharp.getPoint(ch, out num);
                    if ((Math.Abs((int)(num - jiaodu)) >= 0x69) && (Math.Abs((int)(num - jiaodu)) <= 0xff))
                    {
                        ch = this.getPointAnother(sharp, ch);
                        if (ch != 'N')
                        {
                            int num2;
                            Point point2 = sharp.getPoint(ch, out num2);
                            if (pointTo.Equals(point2) && (num2 == jiaodutu))
                            {
                                return true;
                            }
                            Point point = sharp.getPoint(ch, out num);
                            if (this.digui(lll, point, num, pointTo, jiaodutu))
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

        private char getPointAnother(Sharp sharp, char cPointFlag)
        {
            if ("sbDC".Equals(sharp.sharpType))
            {
                bool flag = ((DC)sharp).定反位.Equals('定');
                switch (cPointFlag)
                {
                    case 'D':
                        return (flag ? 'Q' : 'N');

                    case 'F':
                        return (flag ? 'N' : 'Q');

                    case 'Q':
                        return (flag ? 'D' : 'F');
                }
            }
            else if ("sbWCQD".Equals(sharp.sharpType))
            {
                return ((cPointFlag == 'L') ? 'R' : 'L');
            }
            return 'N';
        }

        public int get端点(Dictionary<int, JYJ> dictJyj)
        {
            Dictionary<Point, int> dictionary = new Dictionary<Point, int>();
            Dictionary<Point, Sharp> dictionary2 = new Dictionary<Point, Sharp>();
            Dictionary<Point, int> dictionary3 = new Dictionary<Point, int>();
            Dictionary<Point, char> dictionary4 = new Dictionary<Point, char>();
            this.lst区段端点.Clear();
            Point point = new Point();
            int num = 0;
            foreach (DC dc in this.lst道岔)
            {
                point = dc.getPoint('Q', out num);
                if (dictJyj.ContainsKey((point.X * 0x2710) + point.Y))
                {
                    this.lst区段端点.Add(new Duandian(dc, point, 'Q', num));
                }
                else if (dictionary.ContainsKey(point))
                {
                    dictionary[point] += 1;
                }
                else
                {
                    dictionary.Add(point, 1);
                    dictionary3.Add(point, num);
                    dictionary2.Add(point, dc);
                    dictionary4.Add(point, 'Q');
                }
                point = dc.getPoint('D', out num);
                if (dictJyj.ContainsKey((point.X * 0x2710) + point.Y))
                {
                    this.lst区段端点.Add(new Duandian(dc, point, 'D', num));
                }
                else if (dictionary.ContainsKey(point))
                {
                    dictionary[point] += 1;
                }
                else
                {
                    dictionary.Add(point, 1);
                    dictionary3.Add(point, num);
                    dictionary2.Add(point, dc);
                    dictionary4.Add(point, 'D');
                }
                point = dc.getPoint('F', out num);
                if (dictJyj.ContainsKey((point.X * 0x2710) + point.Y))
                {
                    this.lst区段端点.Add(new Duandian(dc, point, 'F', num));
                }
                else if (dictionary.ContainsKey(point))
                {
                    dictionary[point] += 1;
                }
                else
                {
                    dictionary.Add(point, 1);
                    dictionary3.Add(point, num);
                    dictionary2.Add(point, dc);
                    dictionary4.Add(point, 'F');
                }
            }
            foreach (WCQD wcqd in this.lst无岔区段)
            {
                point = wcqd.getPoint('L', out num);
                if (dictJyj.ContainsKey((point.X * 0x2710) + point.Y))
                {
                    this.lst区段端点.Add(new Duandian(wcqd, point, 'L', num));
                }
                else if (dictionary.ContainsKey(point))
                {
                    dictionary[point] += 1;
                }
                else
                {
                    dictionary.Add(point, 1);
                    dictionary3.Add(point, num);
                    dictionary2.Add(point, wcqd);
                    dictionary4.Add(point, 'L');
                }
                point = wcqd.getPoint('R', out num);
                if (dictJyj.ContainsKey((point.X * 0x2710) + point.Y))
                {
                    this.lst区段端点.Add(new Duandian(wcqd, point, 'R', num));
                }
                else if (dictionary.ContainsKey(point))
                {
                    dictionary[point] += 1;
                }
                else
                {
                    dictionary.Add(point, 1);
                    dictionary3.Add(point, num);
                    dictionary2.Add(point, wcqd);
                    dictionary4.Add(point, 'R');
                }
            }
            foreach (Point point2 in dictionary.Keys)
            {
                if (dictionary[point2] == 1)
                {
                    this.lst区段端点.Add(new Duandian(dictionary2[point2], point2, dictionary4[point2], dictionary3[point2]));
                }
            }
            return this.lst区段端点.Count;
        }

        public override string ToString()
        {
            return string.Concat(new object[] { (this.AD.ToString()).PadRight(4), "= ", this.name.PadRight(10), ", ", this.lst道岔.Count, ", ", this.lst无岔区段.Count });
        }

        public string toStringNode()
        {
            return (this.AD + "," + this.name);
        }

        private bool 是否可达(Duandian duandianFrom, Duandian duandianTo)
        {
            foreach (DC dc in this.lst道岔)
            {
                dc.岔后生成了么 = false;
            }
            foreach (WCQD wcqd in this.lst无岔区段)
            {
                wcqd.岔后生成了么 = false;
            }
            List<Sharp> lll = new List<Sharp>();
            foreach (WCQD wcqd2 in this.lst无岔区段)
            {
                lll.Add(wcqd2);
            }
            foreach (DC dc2 in this.lst道岔)
            {
                lll.Add(dc2);
            }
            return this.digui(lll, duandianFrom.point, (duandianFrom.jiaodu + 180) % 360, duandianTo.point, duandianTo.jiaodu);
        }
    }
}

